package project.pianpian.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import project.pianpian.entity.Order;
import project.pianpian.entity.User;
import project.pianpian.entity.Video;
import project.pianpian.mapper.OrderMapper;
import project.pianpian.mapper.UserMapper;
import project.pianpian.mapper.VideoMapper;
import project.pianpian.utils.PageQueryUtil;
import project.pianpian.utils.PageResult;
import project.pianpian.utils.ReturnUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping(value = "/search")
public class SearchAPI {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private OrderMapper orderMapper;

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public JSONObject listUser(@RequestParam Integer pageNumber,
                           @RequestParam String userName){
        if (pageNumber == null || pageNumber < 1 || userName ==null) {
            return ReturnUtils.fail(500,"查询异常！");
        }
        int currentPage = pageNumber;
        String username = userName;

        PageResult result = new PageResult();
        result.setPageSize(5);

        int totalcount = userMapper.getTotalCount(username);
        double d = totalcount / 5.0;
        int tp = (int) Math.ceil(d);

        result.setTotalPage(tp);
        if (currentPage < 1) currentPage = 1;
        if (currentPage > tp) currentPage = tp;
        result.setCurrPage(currentPage);
        int start = (currentPage - 1) * 5;

        List<User> userList = userMapper.findByName(start, 5, username);

        result.setList(userList);

        return ReturnUtils.success(200,"查询成功", result);
    }

    @RequestMapping(value = "/order", method = RequestMethod.GET)
    public JSONObject listOrder(@RequestParam Integer pageNumber,
                               @RequestParam String Ordertitle){
        if (pageNumber == null || pageNumber < 1 || Ordertitle ==null) {
            return ReturnUtils.fail(500,"查询异常！");
        }
        Map params = new HashMap(8);
        params.put("page", pageNumber);
        if (!StringUtils.isEmpty(Ordertitle)) {
            params.put("limit", Ordertitle);
        }
        PageQueryUtil pageUtil = new PageQueryUtil(params);

        String title = pageUtil.getFactor();
        int currentPage = pageUtil.getPage();
        PageResult result = new PageResult();
        result.setPageSize(5);

        int totalcount = orderMapper.getTotalCount(title);
        double d = totalcount / 5.0;
        int tp = (int) Math.ceil(d);

        result.setTotalPage(tp);
        if (currentPage < 1) currentPage = 1;
        if (currentPage > tp) currentPage = tp;
        result.setCurrPage(currentPage);
        int start = (currentPage - 1) * 5;

        List<Order> orderList = orderMapper.findByTitle(start, 5, title);

        result.setList(orderList);

        return ReturnUtils.success(200,"查询成功", result);
    }

    @RequestMapping(value = "/video", method = RequestMethod.GET)
    public JSONObject listVideo(@RequestParam Integer pageNumber,
                                @RequestParam String Videotitle){
        if (pageNumber == null || pageNumber < 1 || Videotitle ==null) {
            return ReturnUtils.fail(500,"查询异常！");
        }
        Map params = new HashMap(8);
        params.put("page", pageNumber);
        if (!StringUtils.isEmpty(Videotitle)) {
            params.put("limit", Videotitle);
        }
        PageQueryUtil pageUtil = new PageQueryUtil(params);

        String title = pageUtil.getFactor();
        int currentPage = pageUtil.getPage();
        PageResult result = new PageResult();
        result.setPageSize(5);

        int totalcount = videoMapper.getTotalCount(title);
        double d = totalcount / 5.0;
        int tp = (int) Math.ceil(d);

        result.setTotalPage(tp);
        if (currentPage < 1) currentPage = 1;
        if (currentPage > tp) currentPage = tp;
        result.setCurrPage(currentPage);
        int start = (currentPage - 1) * 5;

        List<Video> videoList = videoMapper.findByTitle(start, 5, title);

        result.setList(videoList);

        return ReturnUtils.success(200,"查询成功", result);
    }


}
